package graphics.raytracing;

import static util.MatrixOperationsF.*;
import util.MatrixOperationsF;

public class SimpleRay implements Ray {
	private float[] _origin;
	private int _num;
	public final float[] _direction;
	private float[] _rgbAlpha;
	private LightSource _source;
	private int[] _xy;

	public SimpleRay(float[] origin, float[] direction, int num,
			LightSource source,int[] xy) {
		_direction = cloo(direction);
		_origin = cloo(origin);
		_num = num;
		_source = source;
		_xy=xy;
	}

	public void setRGBAlpha(float[] rgbalpha) {
		_rgbAlpha = cloo(rgbalpha);
	}

	@Override
	public boolean canBeCast() {
		return _num > 0;
	}

	@Override
	public void castOn(Tracable trace, Window window) {
		CastResult result = trace.cast(this);
		if(result==null)
		{
			window.color(_xy[0], _xy[1], ar(0,0,0,1));
			return;
		}
		
		float colorization = multiply(normalize(result.getNormal()),normalize(_source.getDirection()));
		colorization=Math.max(0,colorization);
		float[] color = ar(1, 1, 1, 1);
		color = multiply(color, colorization);
		color[3] = 1;
		window.color(_xy[0],_xy[1],color);
	}

	@Override
	public float[] getOrigin() {
		return _origin;
	}

	@Override
	public float[] getDirection() {
		return _direction;
	}
}
